package com.szcinda.fitness.controller;

import com.szcinda.fitness.dto.AppointmentDto;
import com.szcinda.fitness.persistence.Appointment;
import com.szcinda.fitness.AppointmentService;
import com.szcinda.fitness.controller.utils.Result;
import com.szcinda.fitness.dto.QueryAppointmentParams;
import com.szcinda.fitness.persistence.AppointmentType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/appointment")
public class AppointmentController {

    private final AppointmentService appointmentService;

    public AppointmentController(AppointmentService appointmentService) {
        this.appointmentService = appointmentService;
    }

    @GetMapping("getTraineeAppointment/{openId}")
    public Result<List<AppointmentDto>> getTraineeAppointment(@PathVariable String openId) {
        return Result.success(appointmentService.getAppointmentByTrainee(openId));
    }

    @PostMapping("getCoachAppointment")
    public Result<List<AppointmentDto>> getCoachAppointment(@RequestBody QueryAppointmentParams params) {
        return Result.success(appointmentService.getAppointmentByCoach(params.getOpenId(), params.getAppointmentDate()));
    }

    @GetMapping("checkIsMember/{openId}")
    public Result checkIsMember(@PathVariable String openId){
        if(appointmentService.checkIsMember(openId)){
            return Result.success();
        }else{
            return Result.fail("不是会员");
        }
    }

    //非会员调用方法
    @PostMapping("/save")
    public Result save(@RequestBody Appointment appointment){
        appointment.setAppointmentType(AppointmentType.TEMP);
        appointmentService.save(appointment);
        return Result.success();
    }

    //会员调用方法
    @PostMapping("/memberSave")
    public Result memberSave(@RequestBody Appointment appointment){
        appointment.setAppointmentType(AppointmentType.MEMBER);
        appointmentService.save(appointment);
        return Result.success();
    }
}
